#include "protocol.h"
#include "ets_sys.h"
#include "driver/uart.h"
#include "osapi.h"
#include "config.h"
#include "debug.h"
#include "gpio.h"
#include "user_interface.h"
#include "mem.h"
/******************************************************************************
 * FunctionName : user_set_softap_config
 * Description  : set SSID and password of ESP8266 softAP
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
uint16_t ICACHE_FLASH_ATTR
selfStandartProCRC(uint8_t *data_to_cal,uint8_t data_len)
{
	int i=0;
	uint16_t crc_value=0xffff;
	while(data_len--)
	{
	  crc_value^=*data_to_cal++;
    for(i=0;i<8;i++)
		{
		  if(crc_value&0x0001) crc_value=(crc_value>>1)^0xa001;
		  else crc_value=crc_value>>1;
		}
	}
	return (crc_value);
}
/******************************************************************************
 * FunctionName : user_set_softap_config
 * Description  : set SSID and password of ESP8266 softAP
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
uint8_t ICACHE_FLASH_ATTR
dtProCRC(uint8_t * pro_buff)
{
	int i;
	uint8_t crc_tmp=0;
	for(i=0;i<pro_buff[1]-1;i++)
	{
		crc_tmp+=pro_buff[i];
	}
	crc_tmp=~crc_tmp;
	crc_tmp+=1;
	return crc_tmp;
}
/******************************************************************************
 * FunctionName : user_set_softap_config
 * Description  : set SSID and password of ESP8266 softAP
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
int ICACHE_FLASH_ATTR
blSendData(uint8_t *data_buff,uint16_t cmd,uint16_t data_len,uint16_t version)
{
	int i=0;

	uint8_t *send_buff = (uint8_t*)os_zalloc(255);
    if(send_buff==NULL) return -1;

	send_buff[0]=0xaa;
	send_buff[1]=0x55;
	send_buff[2]=0xaa;
	send_buff[3]=0x55;
	send_buff[4]=data_len>>8;
	send_buff[5]=data_len>>0;
	send_buff[6]=cmd>>8;
	send_buff[7]=cmd>>0;
	send_buff[10]=version>>8;
	send_buff[11]=version>>0;

	for(i=0;i<data_len;i++) send_buff[12+i]=data_buff[i];

	uint16_t bl_crc=selfStandartProCRC(data_buff,data_len);

	send_buff[8]=bl_crc>>8;
	send_buff[9]=bl_crc>>0;

	tx_buff_enq(send_buff,12+data_len);
    //两包数据之间延时发送
	for(i=0;i<100;i++)
	{
		os_delay_us(1000);
		system_soft_wdt_feed();
	}
	//释放内存
	os_free(send_buff);
}
/******************************************************************************
 * FunctionName : user_set_softap_config
 * Description  : set SSID and password of ESP8266 softAP
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
int ICACHE_FLASH_ATTR
proSendWrapper(uint16_t pack_decs,uint16_t msg_type,uint8_t* msg,uint8_t msg_len)
{
	int i=0;
	uint8_t *send_buff = (uint8_t*)os_zalloc(255);
    if(send_buff==NULL) return -1;

    send_buff[0]=0xaa;
    send_buff[1]=0x11+msg_len;
    send_buff[2]=pack_decs>>8;
    send_buff[3]=pack_decs>>0;
	for(i=0;i<8;i++)  send_buff[i+4]=0;
	send_buff[12]=msg_type>>8;
	send_buff[13]=msg_type>>0;
	for(i=0;i<2;i++)  send_buff[i+14]=0;
	if(msg!=NULL)
	{
		for(i=0;i<msg_len;i++) send_buff[i+16]=msg[i];
	}
	send_buff[send_buff[1]-1]=dtProCRC(send_buff);

	tx_buff_enq(send_buff,send_buff[1]);
    //两包数据之间延时发送
	for(i=0;i<100;i++)
	{
		os_delay_us(1000);
		system_soft_wdt_feed();
	}
	//释放内存
	os_free(send_buff);
}
